package com.tianhui.consignortwofei.pattern.orderStatus.status.impl;

import android.text.TextUtils;

import com.fgs.common.api.CommonConfig;
import com.fgs.common.utils.CheckUtil;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.enty.OrderCondition;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderOperateInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailItemInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailLineInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailOperateInfo;
import com.tianhui.consignortwofei.mvp.model.enty.detailItem.DetailRemarkInfo;
import com.tianhui.consignortwofei.pattern.freightCalculation.CalculationController;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.CheckDriverOperate;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.FreightOperate;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.RevokeFreightOperate;
import com.tianhui.consignortwofei.pattern.orderOprate.impl.SaleCarFreightOperate;
import com.tianhui.consignortwofei.pattern.orderStatus.status.IOrderStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @author fgs
 * @date 2020/8/28
 * @description 运费计算
 **/
public class FreightStatus implements IOrderStatus {
    @Override
    public Observable getOrderListObservable(OrderCondition orderCondition) {
        Map<String, String> map = new HashMap<>();
        map.put("limit", "10");
        map.put("offset", "1");
        map.put("order", "desc");
        map.put("sort", "recelvetimeconfirm");
        map.put("isstatus", "9,12");
        map.put("page", orderCondition.page + "");
        if (!TextUtils.isEmpty(orderCondition.searchKey)) {
            if (CheckUtil.isContainsNum(orderCondition.searchKey)) {
                map.put("dispatchno", orderCondition.searchKey);
            } else {
                map.put("destination", orderCondition.searchKey);
            }
        }
        return CommonConfig.getInstance().create(OrderApiService.class).freightList(map);
    }

    @Override
    public Observable getOrderDetailObservable(OrderCondition orderCondition) {
        Map<String, String> map = new HashMap<>();
        map.put("dispatchno", orderCondition.dispatchNo);
        return CommonConfig.getInstance().create(OrderApiService.class).dispatchDetail(map);
    }

    @Override
    public List getOrderDetailList(OrderInfo orderInfo) {
        List list = new ArrayList<>();

        DetailInfo detailInfo;
        DetailItemInfo detailItemInfo;
        DetailRemarkInfo remarkInfo;
        DetailOperateInfo operateInfo;
        DetailLineInfo lineInfo;

        detailInfo = new DetailInfo();

        detailInfo.title = "货源单号：" + orderInfo.billno;
        detailInfo.topLeftText = "调度单号：" + orderInfo.dispatchno;
        detailInfo.topMiddleText = getOrderStatus(orderInfo);

        detailInfo.infoList = new ArrayList<>();
        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交单价：";
        detailItemInfo.content = orderInfo.baseprice + "（元/吨）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交金额：";
        detailItemInfo.content = orderInfo.tradtotalprice + "（元）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交单价（含税）：";
        detailItemInfo.content = orderInfo.tradtaxprice + "（元/吨）";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交金额（含税）：";
        detailItemInfo.content = orderInfo.totaltradtaxprice + "（元）";
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "委托人：";
        detailItemInfo.content = orderInfo.clientperson;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "委托人电话：";
        detailItemInfo.content = orderInfo.clienttel;
        detailInfo.infoList.add(detailItemInfo);

        detailInfo.bottomLeftText = "发单人：" + orderInfo.clientperson;
        detailInfo.bottomRightText = "货物类型：" + orderInfo.goodstypetext;

        list.add(detailInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.spanCount = 2;
        detailInfo.title = "重量信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();
        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "成交重量：";
        detailItemInfo.content = orderInfo.tradweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "装车重量：";
        detailItemInfo.content = orderInfo.tranweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "";
        detailItemInfo.content = "";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "确认装车重量：";
        detailItemInfo.content = orderInfo.tranweightconfirm + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "亏吨重量：";
//        detailItemInfo.content = orderInfo.loseweight + "吨";
        detailItemInfo.content = CalculationController.getInstance().getLoseWeight(orderInfo) + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "收货重量：";
        detailItemInfo.content = orderInfo.recelveweight + "吨";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "";
        detailItemInfo.content = "";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "确认收货重量：";
        detailItemInfo.content = orderInfo.recelveweightconfirm + "吨";
        detailInfo.infoList.add(detailItemInfo);

        list.add(detailInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.operateText = "查看";
        detailInfo.iOrderOperate = new CheckDriverOperate();
        detailInfo.title = "司机信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();
        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "姓名：";
        detailItemInfo.content = orderInfo.drivername + " >>";
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "车牌号：";
        detailItemInfo.content = orderInfo.vehiclenum;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "联系方式：";
        detailItemInfo.content = orderInfo.drivertel;
        detailInfo.infoList.add(detailItemInfo);

        list.add(detailInfo);


        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        detailInfo = new DetailInfo();
        detailInfo.title = "时间信息";
        detailInfo.isShowTitleTip = true;
        detailInfo.infoList = new ArrayList<>();

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "抢单时间：";
        detailItemInfo.content = orderInfo.robbilltime;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "创建时间：";
        detailItemInfo.content = orderInfo.create_time;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "发单时间：";
        detailItemInfo.content = orderInfo.sendordertime;
        detailInfo.infoList.add(detailItemInfo);

        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "装车时间：";
        detailItemInfo.content = orderInfo.trantime;
        detailInfo.infoList.add(detailItemInfo);


        detailItemInfo = new DetailItemInfo();
        detailItemInfo.title = "收货时间：";
//        detailItemInfo.content = orderInfo.recepittime;
        detailItemInfo.content = orderInfo.recelvetime;
        detailInfo.infoList.add(detailItemInfo);
        list.add(detailInfo);

        lineInfo = new DetailLineInfo();
        list.add(lineInfo);

        remarkInfo = new DetailRemarkInfo();
        remarkInfo.title = "备注信息";
        remarkInfo.isShowTitleTip = true;
        StringBuilder remark = new StringBuilder();
        if (!TextUtils.isEmpty(orderInfo.transremark)) {
            remark.append(orderInfo.transremark);
        }
        if (!TextUtils.isEmpty(orderInfo.recelveremark)) {
            remark.append("\n");
            remark.append(orderInfo.recelveremark);
        }
        if (!TextUtils.isEmpty(orderInfo.endorderremark)) {
            remark.append("\n");
            remark.append(orderInfo.endorderremark);
        }
        remarkInfo.remark = remark.toString();
        list.add(remarkInfo);

        operateInfo = new DetailOperateInfo();
        operateInfo.operateInfoList = new ArrayList<>();
        OrderOperateInfo orderOperateInfo;

        if (TextUtils.isEmpty(orderInfo.fcid)) {

            orderOperateInfo = new OrderOperateInfo();
            orderOperateInfo.operate = "运费计算";
            int rolePosition = SharedPreferenceManager.getInstance().getRolePosition();
            if (2 == rolePosition) {
                //2 销售，3物流
                orderOperateInfo.iOrderOperate = new SaleCarFreightOperate();
            } else {
                orderOperateInfo.iOrderOperate = new FreightOperate();
            }
            orderOperateInfo.orderInfo = orderInfo;
            operateInfo.operateInfoList.add(orderOperateInfo);



        } else if (!TextUtils.isEmpty(orderInfo.fcid)) {
            orderOperateInfo = new OrderOperateInfo();
            orderOperateInfo.operate = "撤销计算";
            orderOperateInfo.iOrderOperate = new RevokeFreightOperate();
            orderOperateInfo.orderInfo = orderInfo;
            operateInfo.operateInfoList.add(orderOperateInfo);

            //隐藏付款申请
//            orderOperateInfo = new OrderOperateInfo();
//            orderOperateInfo.operate = "付款申请";
//            orderOperateInfo.iOrderOperate = new PaymentOperate();
//            orderOperateInfo.orderInfo = orderInfo;
//            operateInfo.operateInfoList.add(orderOperateInfo);
        }
        operateInfo.type = 1;
        list.add(operateInfo);

        return list;
    }

    @Override
    public int getOrderStatusType() {
        return OrderType.TYPE_FREIGHT;
    }

    @Override
    public String getOrderStatus(OrderInfo orderInfo) {

        String status = "状态：<font color='#f52225'>" + "未计算" + "</font>";
        if (orderInfo == null) {
            return status;
        }
        if (!TextUtils.isEmpty(orderInfo.iscount) && orderInfo.iscount.equals("1")) {
            status = "状态：<font color='#00ff00'>" + "已计算" + "</font>";
        }
        return status;
    }

    @Override
    public String getDispatchNumber(OrderInfo orderInfo) {
        return orderInfo.dispatchno;
    }

    @Override
    public String getFreight(OrderInfo orderInfo) {
        return null;
    }

    @Override
    public String getSearchHint() {
        return "按调度单号或目的地查询";
    }

    @Override
    public int getFeiOrderType() {
        return 0;
    }
}
